import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  Delete,
  BadRequestException,
} from '@nestjs/common';
import { BooksService } from './books.service';
import { CreateBookDto } from './dto/create-book.dto';
import { UpdateBookDto } from './dto/update-book.dto';
import { BookEntity } from './entities/book.entity';

@Controller('books')
export class BooksController {
  constructor(private readonly booksService: BooksService) {}

  @Post()
  create(@Body() createBookDto: CreateBookDto) {
    return this.booksService.create(createBookDto);
  }

  // 获取书籍列表
  @Get('lists')
  async findAll(
    @Query('pageNum') pageNum: number = 1,
    @Query('pageSize') pageSize: number = 10,
  ): Promise<{
    code: number;
    data: BookEntity[];
    total: number;
  }> {
    return this.booksService.findAll(pageNum, pageSize);
  }

  //获取书籍详情
  @Get('list') //http://localhost:3000/api/text/list?bid=1
  async findOne(
    @Query('bid') bid: number,
    @Query('pageNum') pageNum: number = 1,
    @Query('pageSize') pageSize: number = 10,
  ): Promise<{
    code: number;
    data: BookEntity[];
    total: number;
  }> {
    if (!bid) {
      throw new BadRequestException('缺少必填参数: bid');
    }
    return this.booksService.findOne(bid, pageNum, pageSize);
  }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return this.booksService.findOne(+id);
  // }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateBookDto: UpdateBookDto) {
    return this.booksService.update(+id, updateBookDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.booksService.remove(+id);
  }
}
